import AllStocks_processes_TopX7 as ap
from findOLS.adfall import SQliteQueryTools
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
#dict = None


def showTop10(num,df_result2):
    dict = config()
    #df_result2.iloc[1, 0]
    for i in range(num):

        df = ap.getPAIRdata_memory(df_result2.iloc[i, 2], df_result2.iloc[i, 4],dict)
        #df = getPAIRdata(df_result2.iloc[i, 1], df_result2.iloc[i, 3])
        plot_price_series_stock(df, df_result2.iloc[i, 2], df_result2.iloc[i, 4],dict)

def plot_price_series_stock(df, ts1, ts2, dicto):

    startDatatime = df.index.values[0]
    endDatatime = df.index.values[df.shape[0]-1]
    #print(startDatatime)
    months = mdates.MonthLocator()  # every month
    fig, ax = plt.subplots()
    #fig = plt.figure()
    #ax = plt.subplot(111)
    ax.plot(df.index, df['X'], label=ts1)
    ax.plot(df.index, df['Y'], label=ts2)
    ax.xaxis.set_major_locator(months)
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y')) #%Y-%m-%d
    #ax.set_xlim(datetime.datetime(2012, 1, 1), datetime.datetime(2013, 1, 1))
    ax.set_xlim(startDatatime, endDatatime)
    #ax.set_xlim('2017-12-09', '2018-12-09')
    ax.grid(True)
    fig.autofmt_xdate()

    plt.xlabel('Month/Year')
    plt.ylabel('Price ($)')
    #print('dfsdfdsaf ddddddddd',list[list.code == ts1].iloc[0, 2])

        #namestock2 = list.loc[ts2].iloc[2]
        #print('tttttttttt' , namestock1)
    plt.title('%s and %s Daily Prices' % (ts1, ts2))
    plt.legend()
    plt.rcParams["font.sans-serif"] = ["SimHei"]  # 设置字体
    plt.rcParams["axes.unicode_minus"] = False
    plt.show()

def getPAIRdata_memory222(name1, name2, dict):#数据整理,基于内存
    #global startdate
    #print(dict.get(code1))
    #print(dict)
    startdate = dict.get('startdate')
    enddate = dict.get('enddate')
    if dict.get(name1) is None:#将已经查询的数据放置到内存。
        dict[name1] = SQliteQueryTools.gethistory(name1, startdate, enddate)


    if dict.get(name2) is None:
        dict[name2] = SQliteQueryTools.gethistory(name2, startdate, enddate)

    df_X = dict.get(name1)
    df_Y = dict.get(name2)
    if df_X.shape[0] < 1:
        return df_X
    df = pd.DataFrame(index=df_Y.index)
    if df_Y.shape[0] < 1:
        return df_Y
    df["X"] = df_X["收盘"]
    df["Y"] = df_Y["收盘"]

    df3 = df.sort_index(axis=0, ascending=True)
    #pd.set_option('display.max_rows', None)  # 打印所有行
    df3 = df3.dropna(axis=0, how='any')  # 删除表中任何含有NaN的行
    #print(df3)
    return df3

def config():
    from multiprocessing import Manager
    m = Manager()
    dict = m.dict()
    #print(dict)
    #df_result = pd.DataFrame()#pd.DataFrame(columns=['A', 'B', 'adf'])

    startdate = '2022-01-01'
    enddate = '2023-02-27'
    loopnum = 20 #最大比较次数        # 100000约需要20分钟
    stockRange = 'ALLStocksProcessPools' # 用多线程完成
    dict['startdate'] = startdate
    dict['enddate'] = enddate
    dict['loopnum'] = loopnum
    dict['stockRange'] = stockRange
    return dict

def flow():

    df_result2 = pd.read_csv('last_Result.csv')
    showTop10(5, df_result2)

if __name__ == '__main__':
    #config()

    flow()